package cn.mengfly.product.service.impl;

import cn.mengfly.product.api.UserDto;
import cn.mengfly.product.repository.UserRedisRepository;
import cn.mengfly.product.service.UserRemoteService;
import cn.mengfly.product.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Mengfly
 * @date 2021/3/23 21:54
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {

    @Autowired
    protected UserRemoteService userRemoteService;

    @Autowired
    protected UserRedisRepository userRedisRepository;

    @Override
    public UserDto load(Long id) {
        UserDto userDto = this.userRedisRepository.findOne(id);

        if (null != userDto) {
            log.info("从缓存中获取用户 {}", id);
            return userDto;
        }
        log.info("Redis缓存中不存在目标用户{}", id);
        userDto = this.userRemoteService.load(id);
        if (null != userDto) {
            this.userRedisRepository.saveUser(userDto);
        }
        return userDto;
    }


}
